#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include  <math.h>
void getRandKingdomCards(int *kingdomCards);
int stillAvailable(int card, struct gameState *state);
//int firstTreasure(struct gameState *state);
void getRandKingdomCards(int *kingdomCards)
{
    int i,j, temp;
    for (i=0;i<10;i++)
    {
       temp = (rand() % 20 + 7);
       for (j=0;j<=i;j++)
       {
            if (temp == kingdomCards[j])
            {
                i--;
                break;
            }
            else if (j==i){kingdomCards[i] = temp;}
       }
    }
}
int stillAvailable(int card, struct gameState *state)
{
    if(state->supplyCount[card] > 0){ return 1;}
    else{return 0;}

}
//int firstTreasure(struct gameState *state);
int main(int argc, char *argv[])
{
    struct gameState *state = newGame();
    //int *bonus = malloc(sizeof(int));
    int i,card,temp,temp2,temp3,numPlayers,kingdomCards[10],curPlayer;
    int buyableCards[13];
    //srand(time(0));
    if(argv[1]){srand(atoi(argv[1]));}
    else{srand(time(0));}
    FILE *f;
    f = fopen("gameresults.out","w+");
    //printf("TEST after file open\n");
    //initialize state
    numPlayers = (rand() % 3 + 2);
    getRandKingdomCards(kingdomCards);
    for(i=0;i<10;i++)
    {
        buyableCards[i] = kingdomCards[i];
    }
    buyableCards[10]=copper;
    buyableCards[11]=silver;
    buyableCards[12]=gold;
    //for(i=0;i<13;i++){fprintf(f,"%d: %d\n", i, buyableCards[i]);} //Prints out kingdom cards
    fprintf(f, "Starting game with %d players\n", numPlayers);
    //printf("got past the first print\n");
    initializeGame(numPlayers,kingdomCards,(rand() % 1000),state);

    while(!isGameOver(state))
    {
        curPlayer = whoseTurn(state);
        fprintf(f,"It is now player %d's turn\n", curPlayer);

        while(state->numActions > 0 && state->numBuys > 0)
        {
            fprintf(f,"Player %d's hand is:\n", curPlayer);

            for(i=0;i<state->handCount[curPlayer];i++)
            {
                fprintf(f,"%d ", state->hand[curPlayer][i]);
            }
            fprintf(f,"\n");

            card = rand() % state->handCount[curPlayer]; //Select random card in hand to play
            fprintf(f,"randomness chose card %d\n", card);
            //if card is victory points card, end turn
            if (state->hand[curPlayer][card] < 4)
            {
                fprintf(f,"Player %d chooses to end their turn\n", curPlayer);
                break;
            }
            //if card is money, buy another card
            else if (state->hand[curPlayer][card] < 7)//needs to be refactored with buyableCards
            {
                temp2 = rand()%27+1;
                temp = 0;
                while(buyCard(temp2, state) == -1)
                {
                    fprintf(f,"Tried to buy %d, but there are none left, or you do not have enough money\n", temp2);
                    temp2 = rand()%27+1;
                    temp++;
                    if (temp > 5)
                    {
                        fprintf(f,"Buying cards is hard, ending turn\n");
                        break;
                    }

                }
                if (temp <= 5){
                fprintf(f,"Bought card %d\n", temp2);
                }
            }

            else if(state->hand[curPlayer][card] == feast)
            {
                temp2 = buyableCards[(rand()%13)];
                while(!(stillAvailable(temp2,state)) || (getCost(temp2)>5))
                {
                    fprintf(f,"Tried to gain %d, but there are not left.\n", temp2);
                    temp2 = buyableCards[(rand()%13)];
                }
                fprintf(f,"Played Feast and choose to gain card %d\n", temp2);
                playCard(card,temp2,0,0,state);
                //break;
            }

            else if(state->hand[curPlayer][card] == mine)
            {
                temp  = -1;
                for(i=0;i<state->handCount[curPlayer];i++)
                {
                    if (state->hand[curPlayer][i] == 4  || state->hand[curPlayer][i] == 5)
                    {
                        temp = i;
                        break;
                    }

                }
                if (temp >= 0)
                {
                    fprintf(f,"Playing mine to exchange card in hand %d\n", temp);
                    playCard(card,temp,(state->hand[curPlayer][temp]+1),0,state);
                }
                else {
                    fprintf(f,"No treasure cards for mine to exchange\n");
                    break;
                }
            }

            else if(state->hand[curPlayer][card] == remodel)
            {
                temp = (rand()%(state->handCount[curPlayer]));
                //Mkes sure that you're not trying to trash remodel
                while(temp == card)
                {
                   // fprintf(f,"TEST1 remodel\n");
                    temp = (rand()%(state->handCount[curPlayer]));
                }
                temp2 = buyableCards[rand()%13];
                //Makes sure that replacement card is valid choice
                while(getCost(temp)>getCost(temp2)+2)
                {
                    //fprintf(f,"TEST2 remodel\n");
                    temp2 = buyableCards[rand()%13];
                }
                fprintf(f,"Exchanging card in hand position: %d for a %d\n", temp,temp2);
                playCard(card,temp,temp2,0,state);
            }


            else if(state->hand[curPlayer][card] == steward)
            {
                temp = rand()%3+1;
                if(temp == 3)
                {
                    temp2 = (rand()%(state->handCount[curPlayer]));
                    while(temp2 == card)
                    {
                        temp2 = (rand()%(state->handCount[curPlayer]));
                    }
                    temp3 = (rand()%(state->handCount[curPlayer]));
                    while(temp3 == card || temp3 == temp2)
                    {
                        temp3 = (rand()%(state->handCount[curPlayer]));
                    }
                }
                fprintf(f,"Playing steward with choice %d\n",temp);
                if(temp==3){fprintf(f,"discarding %d and %d\n", temp2, temp3);}
                playCard(card,temp,temp2,temp3,state);
            }

            else if(state->hand[curPlayer][card] == ambassador) {
                fprintf(f,"Ambassador was chosen, but was not played. Ending Turn\n");
                break;
            }

            else if(state->hand[curPlayer][card] == embargo)
            {
                temp = kingdomCards[(rand()%10)];
                //make sure that there's still supply left in that choice
                while(!(stillAvailable(temp,state))){temp = kingdomCards[(rand()%10)];}
                fprintf(f,"Embargo was played on %d", temp);
                playCard(card,temp,0,0,state); }

            else if(state->hand[curPlayer][card] == salvager)
            {
                temp = (rand()%(state->handCount[curPlayer]));
                //Make sure that chosen card isn't salvager
                while(temp == card)
                {
                    temp = (rand()%(state->handCount[curPlayer]));
                }
                fprintf(f,"Salvager was played and %d was chosen", temp);
                playCard(card,temp,0,0,state);
            }
            else if(state->hand[curPlayer][card] == 19)
            {
                fprintf(f,"Tribute card was selected, but is broken wtih an infinite loop. Ending turn \n");
                break;
            }

            else if(state->hand[curPlayer][card] <= 26)
            {
                fprintf(f,"Playing card %d\n", state->hand[curPlayer][card]);
                playCard(card,0,0,0,state);
            }

            else{fprintf(f,"Something broke, aborting test");abort();}

        }
        endTurn(state);

    }//game has ended
    fprintf(f,"Game is Over.\n");
    for(i=0;i<numPlayers;i++)
    {
        fprintf(f,"Player %d: %d\n", i, scoreFor(i,state));
    }
    fclose(f);

    return 0;
}
